app.controller('NewDeviceModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$localStorage', '$state', '$location',
        function ($scope, $modal, $log, $http, $window, $localStorage, $state, $location) {
            $scope.items = ['item1', 'item2', 'item3'];
            
            var product_id,equipment_name,mac_address,equipment_snumber,type;
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
                $scope.oo = {};
                $scope.items = items;
                $scope.selected = {
                    item: $scope.items[0]
                };
                $scope.oo.proname = $state.params.pname;
                $scope.ok = function () {  
                   
                   
                    mac_address= $scope.oo.mac_address;
                    equipment_name = $scope.oo.equipment_name;
                    equipment_snumber = $scope.oo.equipment_snumber;                
                    type = $scope.oo.type;
                           
                    $modalInstance.close($scope.selected.item);
                };
                
                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };
            $scope.open = function (size) {
                var modalInstance = $modal.open({
                    templateUrl: 'newdeviceModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;
                        }
                    }
                });
                modalInstance.result.then(function (selectedItem) {
                    
                    $http.post(
                        $scope.app.backendurl+"tbl-equipment/create",
                        {
                            'product_id':$state.params.pid,                           
                            'mac_address': mac_address,                           
                            'equipment_snumber':equipment_snumber,
                            'type':type, 
                            'equipment_name':equipment_name,                           
                            'token':$localStorage.token                   

                        }
                    ).success(function (data) {
                     //console.log(data);
                      if(data.code==0&&data.msg=="设备创建成功"){
                        alert(data.msg);
                        $window.location.reload('app/sys/devicelist');                      

                      }
                      if(data.code==1){
                        alert(data.msg)
                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        $scope.authError = data.error.code;
                        if ($scope.authError == 401) {
                            $location.path('access.signin');
                        }
                    })
                });
            };
        }])


app.controller('DelDeviceModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$location','$localStorage',
        function ($scope, $modal, $log, $http, $window, $location,$localStorage) {

            $scope.items = ['item1', 'item2', 'item3'];
            var equipment_id;            
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {

                $scope.oo = {};

                $scope.items = items;               
                $scope.selected = {
                    item: $scope.items[0]
                };
                $scope.ok = function () {

                    $modalInstance.close($scope.selected.item);
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };


            $scope.deldev = function (aid) {                
                equipment_id = aid;  
                $scope.open();
            }

            $scope.open = function (size) {

                var modalInstance = $modal.open({
                    templateUrl: 'deldeviceModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;

                        }
                    }
                });

                modalInstance.result.then(function (selectedItem) {                   
                    $http.post(
                        $scope.app.backendurl+"tbl-equipment/delete",
                        {
                           
                            'equipment_id': equipment_id,                                                     
                            'token':$localStorage.token                   

                        }
                    ).success(function (data) {
                       //console.log(data);
                      if(data.code==0&&data.msg=="设备删除成功"){
                        alert(data.msg);
                        $window.location.reload('app/sys/devicelist');

                      }
                      if(data.code==1){
                        alert(data.msg);
                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        $scope.authError = data.error.code;
                        if ($scope.authError == 401) {
                            $location.path('access.signin');
                        }
                    })


                }, function () {
                    $log.info('Modal dismissed at: ' + new Date());
                });
            };
        }])